﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MaxLeaf.Platform.DataContract;
using MaxLeaf.Platform.DALImplementation;
using CommonsLib.DB;
using MaxLeaf.Platform.Functions;
using System.Data.SqlClient;

namespace MaxLeaf.Platform.BRL
{
    public class mDepartmentBRL : BaseBRL
    {

        public override string UniqueFields
        {
            get
            {
                return "CompanyID,DepName";
            }
        }
        public override bool AddValid(DBAccess dbcon, BaseDAL dal, object model)
        {
            if (!base.AddValid(dbcon, dal, model))
            {
                return false;
            }

            if (!CheckParentSeqNo(dbcon, dal, model))
            {
                return false;
            }
            

            return true;
        }

        public override bool UpdateValid(DBAccess dbcon, BaseDAL dal, object model)
        {
            if (!base.UpdateValid(dbcon, dal, model))
            {
                return false;
            }

            if (!CheckParentSeqNo(dbcon, dal, model))
            {
                return false;
            }
             
            return true;
        }

        private bool CheckParentSeqNo(DBAccess dbcon, BaseDAL dal, object model)
        {
            string strSql=string.Empty;
            mDepartmentDataModel department = (mDepartmentDataModel)model;

            if (department.ParentID.Equals(0)) return true;

            bool blnRT = true;
            if (department.ParentID.Equals(department.Key)) blnRT=false;

            if (blnRT)
            {
                strSql = "select top 1 * from mDepartment  where DepID<>@DepID and CompanyID=@CompanyID and DepID=@ParentID";
                using (System.Data.Common.DbDataReader dr = dbcon.GetDataFirstRow(strSql, new SqlParameter[]
                {
                    new SqlParameter("@DepID",department.Key)
                    ,new SqlParameter("@CompanyID",department.CompanyID)
                    ,new SqlParameter("@ParentID",department.ParentID)
                }))
                {
                    if (!dr.Read())
                    {
                        blnRT = false;
                    }
                }
            }

            if (blnRT)
            {
                //子是父的父级
                strSql = "select top 1 * from mDepartment  where dbo.F_ExistParentDepartment(@DepID,@ParentID)=1";
                using (System.Data.Common.DbDataReader dr = dbcon.GetDataFirstRow(strSql, new SqlParameter[]
                {
                    new SqlParameter("@ParentID",department.Key)
                    ,new SqlParameter("@DepID",department.ParentID)
                }))
                {
                    if (dr.Read())
                    {
                        blnRT = false;
                    }
                }
            }

            if (!blnRT)
            {
                throw new MaxLeaf.Platform.Commons.MaxLeafException(string.Format("无效 ParentID {0}!", department.ParentID));
            }
            return blnRT;
        }

      
         
    }
}
